<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="码方案名称" prop="codeName">
        <el-input
          v-model="queryParams.codeName"
          placeholder="请输入码方案名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="码类别" prop="codeType">
        <el-select v-model="queryParams.codeType" placeholder="请选择码类别" clearable>
          <el-option
            v-for="dict in bus_qr_code_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发码机构" prop="codeOrgan">
        <el-input
          v-model="queryParams.codeOrgan"
          placeholder="请输入发码机构"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="码状态" prop="enabled">
        <el-select v-model="queryParams.enabled" placeholder="请选择码状态" clearable>
          <el-option
            v-for="dict in bus_enabled"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['bus:platCodeManagement:add']"
          >新增</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="PlatCodeManagementList" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="码方案名称" align="center" prop="codeName" min-width="150" show-overflow-tooltip />
        <el-table-column label="码方案code" align="center" prop="codeLabel" min-width="150" />
        <el-table-column label="码类别" min-width="100" align="center" prop="codeType">
          <template #default="scope">
            <dict-tag :options="bus_qr_code_type" :value="scope.row.codeType"/>
          </template>
        </el-table-column>
        <el-table-column label="码标准" align="center" prop="codeStandard" min-width="180" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="bus_qr_code_standard" :value="scope.row.codeStandard"/>
          </template>
        </el-table-column>
        <el-table-column label="发码机构" align="center" prop="codeOrgan" min-width="150" show-overflow-tooltip />
        <el-table-column label="更新时间" width="170" align="center" prop="updateTime" />
        <!-- <el-table-column label="码状态" align="center" prop="enabled" width="100">
          <template #default="scope">
            <dict-tag :options="bus_enabled" :value="scope.row.enabled"/>
          </template>
        </el-table-column> -->
        <!-- <el-table-column label="码方案描述" align="center"  prop="remark" /> -->
        <el-table-column label="操作" width="90" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['bus:platCodeManagement:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="查看详情" placement="top">
              <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['bus:platCodeManagement:query']"></el-button>
            </el-tooltip>
            <!-- <el-button link type="primary" icon="Edit" @click="handleKey(scope.row)" v-hasPermi="['bus:platCodeManagement:key']">密钥</el-button> -->
            <!-- <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['bus:platCodeManagement:remove']">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改码方案对话框 -->
    <Dialog v-model="showDialog" :title="dialogTitle" width="60%">
      <edit-code
        v-if="dialogType == 1"
        ref="editCode"
        @cancel="cancel"
        @confirm="confirm"
      />
      <code-config
        v-if="dialogType == 2"
        ref="codeConfig"
        @cancel="cancel"
        @confirm="confirm"
      />
      <code-detail
        v-if="dialogType == 3"
        ref="codeDetail"
        @cancel="cancel"
      />
    </Dialog>
  </div>
</template>

<script setup name="PlatCodeManagement">
import { listPlatCodeManagement, delPlatCodeManagement } from "@/api/bus/PlatCodeManagement"
import { nextTick } from "vue"
import EditCode from './edit.vue'
import CodeConfig from './config.vue'
import CodeDetail from './detail.vue'

const editCode = ref()
const codeConfig = ref()
const codeDetail = ref()
const { proxy } = getCurrentInstance()
const { bus_qr_code_type,bus_enabled,bus_qr_code_standard,bus_delete_flag } = proxy.useDict('bus_qr_code_type','bus_enabled','bus_qr_code_standard','bus_delete_flag')

const PlatCodeManagementList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const showDialog = ref(false)
const dialogTitle = ref('')
const dialogType = ref(0)             // 1新增编辑  2密钥  3详情

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    codeName: null,
    codeLabel: null,
    codeType: null,
    codeStandard: null,
    codeOrgan: null,
    enabled: null,
    codeFlag: null,
    remark:null,
  }
})

const { queryParams } = toRefs(data)

/** 查询码方案列表 */
function getList() {
  loading.value = true
  listPlatCodeManagement(queryParams.value).then(response => {
    PlatCodeManagementList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  showDialog.value = false
  dialogTitle.value = ''
  dialogType.value = 0
}

const confirm = () => {
  cancel()
  getList()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

// 密钥配置
function handleKey(row) {
  showDialog.value = true
  dialogType.value = 2
  dialogTitle.value = '码方案密钥配置'
  nextTick(() => {
    codeConfig.value.pageInit(row.id)
  })
}

/** 新增按钮操作 */
function handleAdd() {
  showDialog.value = true
  dialogType.value = 1
  dialogTitle.value = '添加码方案'
  nextTick(() => {
    editCode.value.pageInit()
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
  showDialog.value = true
  dialogType.value = 1
  dialogTitle.value = '修改码方案'
  nextTick(() => {
    editCode.value.pageInit(_id)
  })
}

function handleDetail(row) {
  showDialog.value = true
  dialogType.value = 3
  dialogTitle.value = '码方案详情'
  nextTick(() => {
    codeDetail.value.pageInit(row.id)
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除该码方案？').then(function() {
    return delPlatCodeManagement(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bus/PlatCodeManagement/export', {
    ...queryParams.value
  }, `码方案_${proxy.getFileImportName()}.xlsx`)
}

getList()
</script>
